
Panels.Main.AddImage = function(id, image)
{
  var tile = $("nav_" + id);

  if (!tile)
    return;

  if (image != "images/transparent.png")
    var src = Utility.Config.Get("Imagepack") + "/foregrounds/" + image;

  else
    var src = "./" + image;

  tile.getChildren("img")[0].setAttribute("src", src);
}

Panels.Main.ClampLevel = function(object, level)
{
  level = level.toInt();
  
  if(level == NaN)
    level = 0;
  
  level = Math.max(level, (object == eBuilding? 1: 500));
  
  return level;
}

Panels.Main.Add = function(objectType, id, x, y, type, level, name, owner) // Creates and adds it to the screen.
{
  switch (objectType)
  {
    case eBuilding:
      var info = Utility.ObjectInfo.Get(eBuilding, name);

      if (info.production.length == 0)
        level = 1;

      var object = new Building(id, x, y, name, owner, level);
      break;

    case ePlanet:
      var info = Utility.ObjectInfo.Get(ePlanet, name);

      var object = new Planet(id, x, y, name, level);
      break;

    case eStarbase:
      var info = Utility.ObjectInfo.Get(eStarbase, { type: type, npc: name == "npc" ? NPC : name });

      var object = new Starbase(id, x, y, type, (name == "npc" ? NPC : owner), level);
      break;

    case eBackground: // FIXME: Add background
      break;

    default:
      return;
  }

  Utility.Object.Add(objectType, object);
  Utility.Tile.DeSelect(id);
  Panels.Main.AddImage(id, info.image);
}

Panels.Main.Create = function(object, id, x, y, type, level, name, owner) // Called when an object is created.
{
  level = Panels.Main.ClampLevel(object, level);

  Panels.Main.Add(object, id, x, y, type, level, name, owner);

  if ($("useSelectedTiles").checked)
  {
    var selected = Utility.Tile.GetSelected();

    for (var i = 0; i < selected.length; i++)
    {
      var tile = selected[i];
      if (Utility.Object.Exists(eNull, tile.id) || tile.type != type) // Object there, skip.
        continue;

      Panels.Main.Add(object, tile.id, tile.x, tile.y, type, level, name, owner);
    }
  }

  HideMenus();
}

Panels.Main.SetLevel = function(object, id, level)
{
  level = Panels.Main.ClampLevel(object, level);
  
  var objectData = Utility.Object.Get(eNull, id);
  objectData[(object == eBuilding? "level": "population")] = level;
  Utility.Object.Update(object, id, objectData);
  
  Panels.Main.ShowInfo(object, id);
}

Panels.Main.DeleteObject = function(object, id)
{
  Utility.Object.Delete(object, id);
  Panels.Main.AddImage(id, "images/transparent.png");
  HideMenus();
}